package com.liuwei.msmall.product.controller.api;

import com.liuwei.msmall.product.entity.CategoryEntity;
import com.liuwei.msmall.product.service.CategoryService;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Classname TestController
 * @Date 2021/4/19 23:11
 * @Created by 刘
 */
@RestController
@RequestMapping("product/test")
public class TestController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redissonClient;

    @GetMapping("/save/{str}")
    public String testRedis(@PathVariable("str") String str){
        redisTemplate.opsForValue().set("hello",str);
        return redisTemplate.opsForValue().get("hello");
    }


    @GetMapping("/cate")
    public List<CategoryEntity> testCate(){
        return categoryService.getCatelogLevel1();
    }


    @GetMapping("/limit")
    public String testLimit(){

        RSemaphore semaphore = redissonClient.getSemaphore("limit");


        boolean b = semaphore.tryAcquire();
        if (b){
            return "ok";
        }else {
            return "error";
        }


    }


}
